<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {html_table_sort_header} function plugin
 *
 * File:       function.html_table_sort_header.php<br />
 * Type:       function<br />
 * Name:       html_table_sort_header<br />
 * Date:       10th Feb 2005<br />
 * Purpose:    generates table headers for cms<br />
 * Input:<br />
 *           - values     (required) - array
 *           - sort_by    (required) - associative array
 *           - dir    (optional) - array default not set
 *           - base  (optional) - ie <br /> or &nbsp;
 * Examples:
 * {html_table_sort_header values=$headers sort_by=$names}

 * @author     Ben Rowe <ben.rowe@sitecubed.com>
 * @version    1.0
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 */
function smarty_function_html_table_sort_header($params, &$smarty)
{

	$base		= preg_replace("/(\?|&)sort_by\=([^&]+)/", '', preg_replace("/(\?|&)dir\=([^&]+)/", '', @$params['base']));
	$sort_by	= $params['sort_by'];
	$dir		= $params['dir'];

	$_html_result = '';

	if(!isset($params['values'])) {

		return;

	}

	$_html_result .= '<tr id="data_table_header">';

	foreach($params['values'] as $v) {

		if(isset($v[1]) && $v[1] != null) {

			if($v[1] != $sort_by) {

				$href = 'href="index.php'. $base .'sort_by='. $v[1] .'&dir=ASC"';

			} else {

				$dir	= strtolower($dir) == 'desc' ? 'ASC' : 'DESC';
				$href	= 'href="index.php'. $base .'sort_by='. $v[1] .'&dir='. $dir .'"';

			}

		} else {

			$href = null;

		}

		if(substr($v[0], 0, 6) == '<input') {

			$_html_result .= '<td nowrap width="'. @$v[2] .'" align="'. @$v[3] .'">'. $v[0] .'</td>';

		} else {

			$_html_result .= '<td nowrap width="'. @$v[2] .'" align="'. @$v[3] .'"><a '. $href .'>'. $v[0] .'</a></td>';

		}

	}

	$_html_result .= '</tr>';

	return $_html_result;

    $name = 'checkbox';
    $values = null;
    $options = null;
    $selected = null;
    $separator = '';
    $labels = true;
    $output = null;

    $extra = '';

    foreach($params as $_key => $_val) {
        switch($_key) {
            case 'name':
            case 'separator':
                $$_key = $_val;
                break;

            case 'labels':
                $$_key = (bool)$_val;
                break;

            case 'options':
                $$_key = (array)$_val;
                break;

            case 'values':
            case 'output':
                $$_key = array_values((array)$_val);
                break;

            case 'checked':
            case 'selected':
                $selected = array_map('strval', array_values((array)$_val));
                break;

            case 'checkboxes':
                $smarty->trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING);
                $options = (array)$_val;
                break;

            default:
                if(!is_array($_val)) {
                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
                } else {
                    $smarty->trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }

    if (!isset($options) && !isset($values))
        return ''; /* raise error here? */

    settype($selected, 'array');


    if (is_array($options)) {

        foreach ($options as $_key=>$_val)
            $_html_result .= smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels);


    } else {
        foreach ($values as $_i=>$_key) {
            $_val = isset($output[$_i]) ? $output[$_i] : '';
            $_html_result .= smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels);
        }

    }

    return $_html_result;

}


?>
